package zzzhc.image.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileFilter;

import zzzhc.image.model.AppModel;
import zzzhc.image.model.RecognizeTestModel;
import zzzhc.image.recognize.ImageRecognizer;

public class RecognizeTestPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private JPanel jPanel = null;

	private JButton loadImageButton = null;

	private JButton startButton = null;

	private JScrollPane jScrollPane = null;

	private JTable jTable = null;

	private JPanel jPanel1 = null;

	private JLabel jLabel = null;

	private JLabel countLabel = null;

	private JLabel jLabel1 = null;

	private JLabel recognizedCountLabel = null;

	private JLabel jLabel3 = null;

	private JLabel errorCountLabel = null;

	private JLabel jLabel5 = null;

	private JLabel correctRateLabel = null;

	private JLabel jLabel7 = null;

	private JLabel speedLabel = null;

	private JProgressBar progressBar = null;

	private RecognizeTestModel recognizeTestModel = new RecognizeTestModel();

	private JButton jButton = null;

	/**
	 * This is the default constructor
	 */
	public RecognizeTestPanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints15 = new GridBagConstraints();
		gridBagConstraints15.gridx = 0;
		gridBagConstraints15.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints15.insets = new Insets(5, 10, 0, 10);
		gridBagConstraints15.gridy = 2;
		GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
		gridBagConstraints4.gridx = 0;
		gridBagConstraints4.fill = GridBagConstraints.BOTH;
		gridBagConstraints4.insets = new Insets(5, 0, 0, 0);
		gridBagConstraints4.gridy = 3;
		GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
		gridBagConstraints3.fill = GridBagConstraints.BOTH;
		gridBagConstraints3.gridy = 1;
		gridBagConstraints3.weightx = 1.0;
		gridBagConstraints3.weighty = 1.0;
		gridBagConstraints3.insets = new Insets(5, 0, 0, 0);
		gridBagConstraints3.gridx = 0;
		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
		gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints2.gridy = 0;
		gridBagConstraints2.insets = new Insets(5, 0, 0, 0);
		gridBagConstraints2.gridx = 0;
		this.setSize(400, 300);
		this.setLayout(new GridBagLayout());
		this.add(getJPanel(), gridBagConstraints2);
		this.add(getJScrollPane(), gridBagConstraints3);
		this.add(getJPanel1(), gridBagConstraints4);
		this.add(getProgressBar(), gridBagConstraints15);
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
			gridBagConstraints16.gridx = 1;
			gridBagConstraints16.gridy = 0;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.gridx = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 2;
			gridBagConstraints.gridy = 0;
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.add(getLoadImageButton(), gridBagConstraints1);
			jPanel.add(getStartButton(), gridBagConstraints);
			jPanel.add(getJButton(), gridBagConstraints16);
		}
		return jPanel;
	}

	/**
	 * This method initializes loadImageButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getLoadImageButton() {
		if (loadImageButton == null) {
			loadImageButton = new JButton();
			loadImageButton.setText("导入图片");
			loadImageButton
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							JFileChooser chooser = new JFileChooser();
							chooser.setFileFilter(new FileFilter() {

								@Override
								public boolean accept(File f) {
									return f.isDirectory();
								}

								@Override
								public String getDescription() {
									return "directory";
								}

							});
							chooser.setCurrentDirectory(new File("."));
							chooser
									.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
							int retVal = chooser
									.showOpenDialog(RecognizeTestPanel.this);
							if (retVal == JFileChooser.APPROVE_OPTION) {
								File dir = chooser.getSelectedFile();
								File[] fs = dir.listFiles();
								Arrays.sort(fs);
								recognizeTestModel.clearAll();
								for (File f : fs) {
									String name = f.getName();
									int index = name.lastIndexOf(".");
									if (index == -1) {
										continue;
									}
									String code = name.substring(0, index);
									try {
										BufferedImage image = ImageIO.read(f);
										if (image == null) {
											continue;
										}
										recognizeTestModel.addColumn(code,
												image);
									} catch (IOException e1) {
									}
								}
								countLabel.setText(String
										.valueOf(recognizeTestModel
												.getRowCount()));
								progressBar.setMaximum(recognizeTestModel
										.getRowCount());
								progressBar.setValue(0);

							}
						}
					});
		}
		return loadImageButton;
	}

	/**
	 * This method initializes startButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getStartButton() {
		if (startButton == null) {
			startButton = new JButton();
			startButton.setText("开始识别");
			startButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					new TestThread().start();
					startButton.setEnabled(false);
				}
			});
		}
		return startButton;
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBorder(BorderFactory.createTitledBorder(null,
					"\u6570\u636e", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Dialog",
							Font.BOLD, 12), new Color(51, 51, 51)));
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getJTable() {
		if (jTable == null) {
			jTable = new JTable(recognizeTestModel);
			jTable.setRowHeight(80);
			jTable.setDefaultRenderer(BufferedImage.class,
					new ImageTableCellRenderer());
			jTable.setDefaultRenderer(String.class,
					new RecognizeTestTableCellRenderer());
		}
		return jTable;
	}

	/**
	 * This method initializes jPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
			gridBagConstraints14.gridx = 1;
			gridBagConstraints14.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints14.insets = new Insets(5, 0, 0, 0);
			gridBagConstraints14.gridy = 4;
			speedLabel = new JLabel();
			speedLabel.setText("0/秒");
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			gridBagConstraints13.gridx = 0;
			gridBagConstraints13.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints13.insets = new Insets(5, 0, 0, 5);
			gridBagConstraints13.gridy = 4;
			jLabel7 = new JLabel();
			jLabel7.setText("识别速度:");
			jLabel7.setHorizontalAlignment(SwingConstants.RIGHT);
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 1;
			gridBagConstraints12.gridwidth = 6;
			gridBagConstraints12.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints12.insets = new Insets(5, 0, 0, 0);
			gridBagConstraints12.gridy = 3;
			correctRateLabel = new JLabel();
			correctRateLabel.setText("0");
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints11.insets = new Insets(5, 0, 0, 5);
			gridBagConstraints11.gridy = 3;
			jLabel5 = new JLabel();
			jLabel5.setText("正确率:");
			jLabel5.setHorizontalAlignment(SwingConstants.RIGHT);
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.gridx = 1;
			gridBagConstraints10.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints10.insets = new Insets(5, 0, 0, 0);
			gridBagConstraints10.gridy = 2;
			errorCountLabel = new JLabel();
			errorCountLabel.setText("0");
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 0;
			gridBagConstraints9.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints9.insets = new Insets(5, 0, 0, 5);
			gridBagConstraints9.gridy = 2;
			jLabel3 = new JLabel();
			jLabel3.setText("识别错误数量:");
			jLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 1;
			gridBagConstraints8.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints8.insets = new Insets(5, 0, 0, 0);
			gridBagConstraints8.gridy = 1;
			recognizedCountLabel = new JLabel();
			recognizedCountLabel.setText("0");
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints7.insets = new Insets(5, 0, 0, 5);
			gridBagConstraints7.gridy = 1;
			jLabel1 = new JLabel();
			jLabel1.setText("已识别数量:");
			jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints6.insets = new Insets(5, 0, 0, 5);
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 1;
			gridBagConstraints5.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints5.insets = new Insets(5, 0, 0, 0);
			gridBagConstraints5.gridy = 0;
			countLabel = new JLabel();
			countLabel.setText("0");
			jLabel = new JLabel();
			jLabel.setText("图片数量:");
			jLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.setBorder(BorderFactory.createTitledBorder(null,
					"\u7edf\u8ba1\u7ed3\u679c",
					TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Dialog",
							Font.BOLD, 12), new Color(51, 51, 51)));
			jPanel1.add(jLabel, gridBagConstraints6);
			jPanel1.add(countLabel, gridBagConstraints5);
			jPanel1.add(jLabel1, gridBagConstraints7);
			jPanel1.add(recognizedCountLabel, gridBagConstraints8);
			jPanel1.add(jLabel3, gridBagConstraints9);
			jPanel1.add(errorCountLabel, gridBagConstraints10);
			jPanel1.add(jLabel5, gridBagConstraints11);
			jPanel1.add(correctRateLabel, gridBagConstraints12);
			jPanel1.add(jLabel7, gridBagConstraints13);
			jPanel1.add(speedLabel, gridBagConstraints14);
		}
		return jPanel1;
	}

	/**
	 * This method initializes progressBar
	 * 
	 * @return javax.swing.JProgressBar
	 */
	private JProgressBar getProgressBar() {
		if (progressBar == null) {
			progressBar = new JProgressBar();
		}
		return progressBar;
	}

	private class TestThread extends Thread {

		public void run() {
			try {
				ImageRecognizer recognizer = AppModel.getInstance()
						.getImageRecognizer();
				recognizeTestModel.setStartTestTime(System.currentTimeMillis());
				int count = recognizeTestModel.getRowCount();
				for (int i = 0; i < count; i++) {
					BufferedImage image = recognizeTestModel.getImage(i);
					String result = recognizer.recognize(image);
					updateUI(i, result);
				}
			} finally {
				SwingUtilities.invokeLater(new Runnable() {

					public void run() {
						startButton.setEnabled(true);
					}

				});
			}
		}

		private void updateUI(final int index, final String result) {
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					recognizeTestModel.setRecognizeResult(index, result);
					int recognizedCount = recognizeTestModel
							.getRecognizedCount();
					int errorCount = recognizeTestModel.getErrorCount();
					double correctRate = recognizeTestModel.correctRate();
					double speed = recognizeTestModel.getSpeed();
					recognizedCountLabel.setText(String
							.valueOf(recognizedCount));
					errorCountLabel.setText(String.valueOf(errorCount));
					correctRateLabel.setText(String.valueOf(correctRate * 100)
							+ "%");
					speedLabel.setText(speed + "/秒");
					progressBar.setValue(recognizedCount);
					jTable.updateUI();
				}
			});
		}
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("清除");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					recognizeTestModel.clear();
				}
			});
		}
		return jButton;
	}

	public static void main(String[] args) {
		PanelLauncher pl = new PanelLauncher(new RecognizeTestPanel());
		pl.show(400, 300);
	}

}
